package com.example.demo.controller;

import com.example.demo.common.OkHttpUtils;
import com.example.demo.common.PropertyUtil;
import com.example.demo.common.SessionAndGetRemote;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "goSQGL")
public class GoSqController {
    private static String BACKURL = PropertyUtil.getValue("backurl");

    @Autowired
    private OkHttpUtils okHttpUtils;
    /**
     * @Description 初始化界面 查看是否存有session 并获取所有门店
     * @Author zhangxue
     * @Date 16:29 2018/7/11
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initSqPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        String bm = session.getAttribute("bm")!=null?session.getAttribute("bm").toString():null;
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("username",username);
        tMap.put("store",Store);
        tMap.put("userno",userno);
        tMap.put("bm",bm);
        JSONObject js = new JSONObject();
        if(username==null||Store==null){
            tMap.put("status","0");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }else{
            JSONObject jsonObject = JSONObject.fromObject(tMap);
            Map<String,String> headersParams = new HashMap<String,String>();
            headersParams.put("Content-Type","application/json;UTF-8");
            Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/initSqPage",headersParams,jsonObject.toString(),new Object());
            statusMap.put("status","1");
            statusMap.put("store",Store);
            statusMap.put("username",username);
            statusMap.put("userno",userno);
            statusMap.put("bm",bm);
            js = JSONObject.fromObject(statusMap);
            return js.toString();
        }
    }

    /**
         * @Description 根据店面查询所有部门
         * @Author zhangxue
         * @Date 11:39 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllBmByStore",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String changeProduct(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryAllBmByStore";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 根据部门查询所有员工
         * @Author zhangxue
         * @Date 11:39 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllUserByBm",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllUserByBm(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryAllUserByBm";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 授权
         * @Author zhangxue
         * @Date 14:49 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/shouquan",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String shouquan(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/shouquan";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 根据员工编号查询权限
         * @Author zhangxue
         * @Date 15:40 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/getLimByUserNo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getLimByUserNo(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/getLimByUserNo";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 删除授权
         * @Author zhangxue
         * @Date 16:19 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/delLim",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delLim(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/delLim";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 查询所有人员权限列表
         * @Author zhangxue
         * @Date 17:15 2018/10/1
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllLimInfos",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllLimInfos(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryAllLimInfos";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }
}
